//
//  DCFLoginHomeViewController.m
//  DCFactory
//
//  Created by 吴文拼 on 2018/3/30.
//  Copyright © 2018年 吴文拼. All rights reserved.
//

#import "DCFLoginHomeViewController.h"
#import "DCFLoginInputView.h"
#import <IQKeyboardManager.h>
#import "DCFSeeCtrlView.h"
#import "NSString+CommonUseTool.h"
#import <YYText.h>
#import "DCFNetworkManager.h"
#import "UIView+DCFToast.h"
#import "DCFAlertView.h"
#import "NSString+MD5.h"
#import "DCFLoginManager.h"


typedef enum : NSUInteger {
    LoginPageStateNormal,
    LoginPageStateWithPwd,
    LoginPageStateRegister,
    LoginPageStateChangePwd,
} LoginPageState;

@interface DCFLoginHomeViewController ()<DCFAlertViewDelegate>

@property (nonatomic , strong)DCFNormalLabel *titleLab;

@property (nonatomic , strong)DCFNormalLabel *codeAlertLab;

@property (nonatomic , strong)DCFLoginInputView *mobileInputView;

@property (nonatomic , strong)DCFLoginInputView *pwdInputView;

@property (nonatomic , strong)DCFLoginInputView *codeInputView;
@property (nonatomic , strong)DCFNormalLabel *getCodeBtn;//获取验证码按钮

@property (nonatomic , strong)DCFButton *forgetBtn;

@property (nonatomic , assign)LoginPageState pageState;

@property (nonatomic , strong)YYLabel *voiceCodeLab;//语音

@property (nonatomic , strong)DCFButton *nextBtn;

@end

@implementation DCFLoginHomeViewController
{
    NSTimer *_timer;
    NSInteger _timeCount;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    DCFButton *cancelBtn = [DCFButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn set_dcf_Image:[UIImage imageNamed:@"login_cancel"] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(13);
        make.top.equalTo(self.view).offset(30);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(24, 24)]);
    }];
    [self.view addSubview:self.titleLab];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(27.5f);
        make.top.equalTo(self.view).offset(84);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(80, 28)]);
    }];
    [self.view addSubview:self.codeAlertLab];
    [_codeAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleLab);
        make.top.equalTo(_titleLab.mas_bottom).offset(5);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(300, 21)]);
    }];
    [self.view addSubview:self.mobileInputView];
    [self.mobileInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLab.mas_bottom).offset(35);
        make.left.equalTo(self.view).offset(27.5f);
        make.right.equalTo(self.view).offset(-27.5f);
        make.height.equalTo(@54);
    }];
    [self.view addSubview:self.pwdInputView];
    [self.pwdInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mobileInputView.mas_bottom);
        make.left.equalTo(self.view).offset(27.5f);
        make.right.equalTo(self.view).offset(-27.5f);
        make.height.equalTo(@54);
    }];
    [self.view addSubview:self.codeInputView];
    [self.codeInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.pwdInputView.mas_bottom);
        make.left.equalTo(self.view).offset(27.5f);
        make.right.equalTo(self.view).offset(-27.5f);
        make.height.equalTo(@54);
    }];
    [self.view addSubview:self.voiceCodeLab];
    [_voiceCodeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.codeInputView.mas_bottom).offset(10);
        make.left.equalTo(self.codeInputView).offset(8);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(280, 17)]);
    }];
    [self.view addSubview:self.nextBtn];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.codeInputView.mas_bottom).offset(53);
        make.centerX.equalTo(self.view);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(SCREEN_WIDTH-50, 44)]);
    }];
    [self.view addSubview:self.forgetBtn];
    [_forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_nextBtn);
        make.top.equalTo(_nextBtn.mas_bottom).offset(15);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(70, 17)]);
    }];
    
    WEAKSELF
    DCFSeeCtrlView *seeView = [[DCFSeeCtrlView alloc] initWithSecurityBlock:^(BOOL isSecurity) {
        weakSelf.pwdInputView.inputTextField.secureTextEntry = isSecurity;
    }];
    [_pwdInputView addSubview:seeView];
    [seeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_pwdInputView);
        make.bottom.equalTo(_pwdInputView).offset(-16);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(17, 10)]);
    }];
    
    [_codeInputView addSubview:self.getCodeBtn];
    [_getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_codeInputView);
        make.bottom.equalTo(_codeInputView).offset(-14);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(80, 21)]);
    }];
    DCFNormalView *lineView = [[DCFNormalView alloc] init];
    [lineView set_dcf_backgroundColor:RGBCOLOR(221, 221, 221)];
    [_codeInputView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_getCodeBtn.mas_left).offset(-10);
        make.centerY.equalTo(_getCodeBtn);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(1, 14)]);
    }];
    
    self.pageState = LoginPageStateNormal;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    
    NSMutableString *phoneNum = [[DCFUserDefaults objectForKey:@"FirstInputPhoneNumber"] mutableCopy];
    if ([NSString isPhoneNumber:phoneNum]) {
        if (phoneNum.length > 7) {
            [phoneNum insertString:@" " atIndex:7];
            [phoneNum insertString:@" " atIndex:3];
        }
        self.mobileInputView.inputTextField.text = [phoneNum copy];
        self.nextBtn.enabled = YES;
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)cancelClick{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)textFieldDidChange:(NSNotification *)notify{
    UITextField *textField = notify.object;
    if ([textField isEqual:self.mobileInputView.inputTextField]) {
        //号码中间加减空格
        if (self.mobileInputView.inputTextField.text.length == 4) {
            if ([self.mobileInputView.inputTextField.text characterAtIndex:3] == ' ') {
                self.mobileInputView.inputTextField.text = [self.mobileInputView.inputTextField.text substringToIndex:3];
            }else{
                NSMutableString *mutStr = [self.mobileInputView.inputTextField.text mutableCopy];
                [mutStr insertString:@" " atIndex:3];
                self.mobileInputView.inputTextField.text = [mutStr copy];
            }
        }
        if (self.mobileInputView.inputTextField.text.length == 9) {
            if ([self.mobileInputView.inputTextField.text characterAtIndex:8] == ' ') {
                self.mobileInputView.inputTextField.text = [self.mobileInputView.inputTextField.text substringToIndex:8];
            }else{
                NSMutableString *mutStr = [self.mobileInputView.inputTextField.text mutableCopy];
                [mutStr insertString:@" " atIndex:8];
                self.mobileInputView.inputTextField.text = [mutStr copy];
            }
        }
        
        //号码加上两个空格限制在13位
        if (self.mobileInputView.inputTextField.text.length > 13) {
            self.mobileInputView.inputTextField.text = [self.mobileInputView.inputTextField.text substringToIndex:13];
        }
        //是否为正确的手机号
        if (self.mobileInputView.inputTextField.text.length == 13) {
            NSString *phoneNum = [self.mobileInputView.inputTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
            BOOL isPhoneNum = [NSString isPhoneNumber:phoneNum];
            if (isPhoneNum) {
                self.nextBtn.enabled = YES;
            }else{
                self.pageState = LoginPageStateNormal;
            }
        }else{
            self.pageState = LoginPageStateNormal;
        }
    }else if ([textField isEqual:self.pwdInputView.inputTextField]) {
        if (self.pwdInputView.inputTextField.text.length > 16) {
            self.pwdInputView.inputTextField.text = [self.pwdInputView.inputTextField.text substringToIndex:16];
        }
    }
    switch (self.pageState) {
        case LoginPageStateNormal:
            
            break;
        case LoginPageStateWithPwd:
        {
            self.nextBtn.enabled = (self.pwdInputView.inputTextField.text.length > 0)?YES:NO;
        }
            break;
        case LoginPageStateRegister:
        {
            self.nextBtn.enabled = (self.pwdInputView.inputTextField.text.length > 0&&self.codeInputView.inputTextField.text.length>0)?YES:NO;
        }
            break;
        case LoginPageStateChangePwd:
        {
            self.nextBtn.enabled = (self.pwdInputView.inputTextField.text.length > 0&&self.codeInputView.inputTextField.text.length>0)?YES:NO;
        }
            break;
            
        default:
            break;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager];
    keyboardManager.shouldResignOnTouchOutside = YES;
    keyboardManager.enable = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager];
    keyboardManager.shouldResignOnTouchOutside = NO;
    keyboardManager.enable = NO;
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
}

-(DCFNormalLabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [[DCFNormalLabel alloc] init];
        [_titleLab set_dcf_textAligment:NSTextAlignmentLeft];
        [_titleLab set_dcf_font:[UIFont fontWithName:@"PingFangSC-Medium" size:20.0f]?[UIFont fontWithName:@"PingFangSC-Medium" size:20.0f]:GetFontWithSize(20)];
        [_titleLab set_dcf_textColor:RGBCOLOR(51, 51, 51)];
        _titleLab.text = @"注册登录";
    }
    return _titleLab;
}

-(DCFNormalLabel *)codeAlertLab{
    if (!_codeAlertLab) {
        _codeAlertLab = [[DCFNormalLabel alloc] init];
        [_codeAlertLab set_dcf_font:GetFontWithSize(15)];
        [_codeAlertLab set_dcf_textColor:RGBCOLOR(102, 102, 102)];
        [_codeAlertLab set_dcf_textAligment:NSTextAlignmentLeft];
        _codeAlertLab.hidden = YES;
    }
    return _codeAlertLab;
}

-(DCFLoginInputView *)mobileInputView{
    if (!_mobileInputView) {
        _mobileInputView = [[DCFLoginInputView alloc] initWithPlaceText:@"请输入您的手机号码"];
        [_mobileInputView setKeyboardType:UIKeyboardTypeNumberPad];
    }
    return _mobileInputView;
}

-(DCFLoginInputView *)pwdInputView{
    if (!_pwdInputView) {
        _pwdInputView = [[DCFLoginInputView alloc] initWithPlaceText:@""];
        _pwdInputView.inputTextField.secureTextEntry = YES;
    }
    return _pwdInputView;
}

-(DCFLoginInputView *)codeInputView{
    if (!_codeInputView) {
        _codeInputView = [[DCFLoginInputView alloc] initWithPlaceText:@"请输入短信验证码"];
        [_codeInputView setKeyboardType:UIKeyboardTypeNumberPad];
    }
    return _codeInputView;
}

-(DCFNormalLabel *)getCodeBtn{
    if (!_getCodeBtn) {
        _getCodeBtn = [[DCFNormalLabel alloc] init];
        _getCodeBtn.userInteractionEnabled = YES;
        [_getCodeBtn set_dcf_textAligment:NSTextAlignmentCenter];
        [_getCodeBtn set_dcf_font:GetFontWithSize(15)];
        [_getCodeBtn set_dcf_textColor:kMainColor];
        _getCodeBtn.text = @"获取验证码";
        [_getCodeBtn set_dcf_EnlargeEdgeWithTop:10 right:30 bottom:10 left:30];
        [_getCodeBtn addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getCodeClick)]];
    }
    return _getCodeBtn;
}
-(void)getCodeClick{
    NSString *phoneNum = [self.mobileInputView.inputTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    _getCodeBtn.userInteractionEnabled = NO;
    NSString *url = @"";
    NSDictionary *dic = @{};
    if (self.pageState == LoginPageStateRegister) {
        url = DCF_LOGIN_PART_GET_REGISTER_CODE;
        dic = @{@"phoneNumber":phoneNum};
    }else{
        url = DCF_LOGIN_PART_USER_RESETPWD_GET_CODE;
        dic = @{@"phoneNum":phoneNum};
    }
    [DCFNetworkManager postDCFTo:url withJSON:dic successHandler:^(DCFResponseModel *response) {
        if (response.success) {
            _timeCount = 60;
            [_getCodeBtn set_dcf_textColor:RGBCOLOR(153, 153, 153)];
            _getCodeBtn.text = [NSString stringWithFormat:@"重发(%lds)",_timeCount];
            _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timeCode) userInfo:nil repeats:YES];
            _codeAlertLab.hidden = NO;
            _codeAlertLab.text = [NSString stringWithFormat:@"已向%@手机发送验证码",phoneNum];
        }else{
            _getCodeBtn.userInteractionEnabled = YES;
            [self.view makeToast:response.message duration:1.5f position:CSToastPositionCenter];
        }
    } failHandler:^(NSError *error) {
        _getCodeBtn.userInteractionEnabled = YES;
        [self.view makeToast:DCF_NORMAL_UNLINK_NET_TOAST duration:1.5f position:CSToastPositionCenter];
    }];
}
-(void)timeCode{
    _timeCount--;
    if (_timeCount > 0) {
        _getCodeBtn.text = [NSString stringWithFormat:@"重发(%lds)",_timeCount];
    }else{
        [_timer invalidate];
        _timer = nil;
        [_getCodeBtn set_dcf_textColor:kMainColor];
        _getCodeBtn.text = @"获取验证码";
        _getCodeBtn.userInteractionEnabled = YES;
    }
}

-(DCFButton *)forgetBtn{
    if (!_forgetBtn) {
        _forgetBtn = [DCFButton buttonWithType:UIButtonTypeCustom];
        [_forgetBtn set_dcf_Title:@"忘记密码?" forState:UIControlStateNormal];
        [_forgetBtn set_dcf_TitleColor:RGBCOLOR(153, 153, 153) forState:UIControlStateNormal];
        [_forgetBtn.titleLabel setFont:GetFontWithSize(12)];
        [_forgetBtn addTarget:self action:@selector(forgetBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _forgetBtn.hidden = YES;
    }
    return _forgetBtn;
}

-(void)forgetBtnClick{
    self.pageState = LoginPageStateChangePwd;
}

-(YYLabel *)voiceCodeLab{
    if (!_voiceCodeLab) {
        NSMutableAttributedString  *text1 = [[NSMutableAttributedString alloc] initWithString:@"长时间收不到验证码，可尝试语音接听验证码"];
        text1.yy_font = [UIFont systemFontOfSize:12];
        text1.yy_alignment = NSTextAlignmentLeft;
        text1.yy_color = RGBCOLOR(102, 102, 102);
        [text1 yy_setColor:kMainColor range:NSMakeRange(13, 7)];
        
        [text1 yy_setTextHighlightRange:NSMakeRange(13, 7)//设置点击的位置
                                  color:kMainColor
                        backgroundColor:[UIColor groupTableViewBackgroundColor]
                              tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect)
         {
             NSLog(@"这里是点击事件");
         }];
        YYLabel *titleLb                         = [[YYLabel alloc] init];
        titleLb.userInteractionEnabled  = YES;
        titleLb.numberOfLines           = 1;
        titleLb.attributedText = text1;
        titleLb.textVerticalAlignment   = YYTextVerticalAlignmentCenter;
        titleLb.backgroundColor = [UIColor clearColor];
        _voiceCodeLab = titleLb;
        _voiceCodeLab.hidden = YES;
    }
    return _voiceCodeLab;
}

-(void)setPageState:(LoginPageState)pageState{
    _pageState = pageState;
    switch (pageState) {
        case LoginPageStateNormal:
        {
            [self changeNextBtnText:@"下一步"];
            [self.pwdInputView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@0);
            }];
            [self.codeInputView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@0);
            }];
            self.forgetBtn.hidden = YES;
            self.voiceCodeLab.hidden = YES;
            self.codeAlertLab.hidden = YES;
            self.titleLab.text = @"注册登录";
        }
            break;
        case LoginPageStateWithPwd:
        {
            [self changeNextBtnText:@"登录"];
            [self.pwdInputView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@54);
            }];
            [self.codeInputView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@0);
            }];
            [self.pwdInputView setPlaceText:@"请输入登录密码"];
            self.forgetBtn.hidden = NO;
            self.voiceCodeLab.hidden = YES;
            self.titleLab.text = @"注册登录";
        }
            break;
        case LoginPageStateRegister:
        {
            [self changeNextBtnText:@"注册"];
            [self.pwdInputView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@54);
            }];
            [self.codeInputView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@54);
            }];
            [self.pwdInputView setPlaceText:@"设置密码(6～18位数字和字母组合)"];
            self.forgetBtn.hidden = YES;
            self.titleLab.text = @"注册登录";
        }
            break;
        case LoginPageStateChangePwd:
        {
            [self changeNextBtnText:@"登录"];
            [self.pwdInputView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@54);
            }];
            [self.codeInputView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@54);
            }];
            [self.pwdInputView setPlaceText:@"设置新密码(6～18位数字和字母组合)"];
            self.forgetBtn.hidden = YES;
            self.titleLab.text = @"找回密码";
        }
            break;
            
        default:
            break;
    }
}


-(DCFButton *)nextBtn{
    if (!_nextBtn) {
        _nextBtn = [DCFButton buttonWithType:UIButtonTypeCustom];
        [_nextBtn set_dcf_BackgroundColor:RGBCOLOR(221, 221, 221) forState:UIControlStateDisabled];
        [_nextBtn set_dcf_BackgroundColor:kMainColor forState:UIControlStateNormal];
        _nextBtn.clipsToBounds = YES;
        [_nextBtn set_dcf_layerCornerRadius:2.0f];
        _nextBtn.enabled = NO;
        [_nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextBtn;
}
-(void)changeNextBtnText:(NSString *)text{
    [self.nextBtn set_dcf_AttributedTitle:[[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:GetFontWithSize(16),NSKernAttributeName:@2.0f,NSForegroundColorAttributeName:RGBCOLOR(187, 187, 187)}] forState:UIControlStateDisabled];
    [self.nextBtn set_dcf_AttributedTitle:[[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:GetFontWithSize(16),NSKernAttributeName:@2.0f,NSForegroundColorAttributeName:[UIColor whiteColor]}] forState:UIControlStateNormal];
}

-(void)nextBtnClick:(DCFButton *)button{
    [self.view endEditing:YES];
    if (self.pageState == LoginPageStateNormal) {
        [self checkAccount];
    }else if (self.pageState == LoginPageStateRegister) {
        [self userRegister];
    }else if (self.pageState == LoginPageStateWithPwd) {
        [self userLogin];
    }else if (self.pageState == LoginPageStateChangePwd) {
        [self userChangePwd];
    }
}

-(void)checkAccount{
    NSString *phoneNum = [self.mobileInputView.inputTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    [DCFNetworkManager postDCFTo:DCF_LOGIN_PART_CHECK_PHONENUMBER withJSON:@{@"phoneNum":phoneNum} successHandler:^(DCFResponseModel *response) {
        if (response.success) {
            if ([response.responseData boolValue]) {
                self.pageState = LoginPageStateWithPwd;
            }else{
                self.pageState = LoginPageStateRegister;
            }
            self.nextBtn.enabled = NO;
        }else{
            [self.view makeCenterToastWithShowMessage:response.message];
        }
    } failHandler:^(NSError *error) {
        [self.view makeCenterToastWithShowMessage:DCF_NORMAL_UNLINK_NET_TOAST];
    }];
}

-(void)userRegister{
    NSString *pwdStr = self.pwdInputView.inputTextField.text;
    if (pwdStr.length < 6 || pwdStr.length > 16 || ![NSString stringIsOnlyInNumberAndLetter:pwdStr]) {
        DCFAlertView *alertView = [[DCFAlertView alloc] initWithDelegate:self withTitleText:@"密码格式不规范" andDetailText:@"由[6~16位]字母和数字组成" andBottomBtnTitleArr:@[@"我知道了"]];
        [alertView showInView:self.view];
        return;
    }
    NSString *phoneNum = [self.mobileInputView.inputTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *codeStr = self.codeInputView.inputTextField.text;
    [DCFNetworkManager postDCFTo:DCF_LOGIN_PART_USER_REGISTER withJSON:@{@"mobileNum":phoneNum,@"password":[NSString MD5FromString:pwdStr],@"verifyCode":codeStr} successHandler:^(DCFResponseModel *response) {
        if (response.success) {
            DCFUserModel *model = [[DCFUserModel alloc] initWithDic:response.responseData];
            [[DCFLoginManager sharedInstance] saveLoginUserModelInfo:model];
            [self dismissViewControllerAnimated:YES completion:^{
                [[UIApplication sharedApplication].keyWindow makeCenterToastWithShowMessage:@"注册成功"];
            }];
        }else{
            [self.view makeCenterToastWithShowMessage:response.message];
        }
    } failHandler:^(NSError *error) {
        [self.view makeCenterToastWithShowMessage:DCF_NORMAL_UNLINK_NET_TOAST];
    }];
}

-(void)userLogin{
    NSString *pwdStr = self.pwdInputView.inputTextField.text;
    NSString *phoneNum = [self.mobileInputView.inputTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    [DCFNetworkManager postDCFTo:DCF_LOGIN_PART_USER_LOGIN withJSON:@{@"mobileNum":phoneNum,@"password":[NSString MD5FromString:pwdStr]} successHandler:^(DCFResponseModel *response) {
        if (response.success) {
            DCFUserModel *model = [[DCFUserModel alloc] initWithDic:response.responseData];
            [[DCFLoginManager sharedInstance] saveLoginUserModelInfo:model];
            [self dismissViewControllerAnimated:YES completion:^{
                [[UIApplication sharedApplication].keyWindow makeCenterToastWithShowMessage:@"登录成功"];
            }];
        }else{
            [self.view makeCenterToastWithShowMessage:response.message];
        }
    } failHandler:^(NSError *error) {
        [self.view makeCenterToastWithShowMessage:DCF_NORMAL_UNLINK_NET_TOAST];
    }];
}

-(void)userChangePwd{
    NSString *pwdStr = self.pwdInputView.inputTextField.text;
    if (pwdStr.length < 6 || pwdStr.length > 16 || ![NSString stringIsOnlyInNumberAndLetter:pwdStr]) {
        DCFAlertView *alertView = [[DCFAlertView alloc] initWithDelegate:self withTitleText:@"密码格式不规范" andDetailText:@"由[6~16位]字母和数字组成" andBottomBtnTitleArr:@[@"我知道了"]];
        [alertView showInView:self.navigationController.view];
        return;
    }
    NSString *phoneNum = [self.mobileInputView.inputTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *codeStr = self.codeInputView.inputTextField.text;
    [DCFNetworkManager postDCFTo:DCF_LOGIN_PART_USER_RESET_PWD withJSON:@{@"phoneNumber":phoneNum,@"newPassword":[NSString MD5FromString:pwdStr],@"verifyCode":codeStr} successHandler:^(DCFResponseModel *response) {
        if (response.success) {
            DCFUserModel *model = [[DCFUserModel alloc] initWithDic:response.responseData];
            [[DCFLoginManager sharedInstance] saveLoginUserModelInfo:model];
            [self dismissViewControllerAnimated:YES completion:^{
                [[UIApplication sharedApplication].keyWindow makeCenterToastWithShowMessage:@"修改密码成功"];
            }];
        }else{
            [self.view makeCenterToastWithShowMessage:response.message];
        }
    } failHandler:^(NSError *error) {
        [self.view makeCenterToastWithShowMessage:DCF_NORMAL_UNLINK_NET_TOAST];
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
